#include"stdafx.h"
#include "Regex.h"

Regex::~Regex()
{
	if (re!=nullptr)
		delete re;
}

Regex Regex::Create(string pattern, string input, bool ignoreCase, bool multiline)
{
	Regex *temp = new Regex();

	temp->re = new regex(pattern);
	sregex_iterator ip_it(input.begin(), input.end(), *temp->re);
	sregex_iterator ip_end;
	for (; ip_it != ip_end; ++ip_it)
	{
		if (temp->count == 0)
			temp->groupCounts = ip_it->size() - 1;

		temp->matchs.push_back(ip_it->str());
		if (temp->groupCounts>0)
		{
			temp->matchGroups.push_back(*new vector<string>());
			for (int j = 0; j < temp->groupCounts; j++)
			{
				temp->matchGroups.at(temp->count).push_back(ip_it->str(j + 1));
			}
		}
		temp->count++;
	}
	return *temp;
}

int Regex::GetMatchesCount()
{
	return count;
}

string Regex::GetMatchesValue(int index)
{
	if (index<0 || index>count-1)
	{
		return "";
	}
	return matchs.at(index);
}

string Regex::GetMatchesGroupsValue(int index, int group)
{
	if (index<0 || index>count - 1)
	{
		return "";
	}
	if (group<0 || group>groupCounts)
	{
		return "";
	}
	return matchGroups.at(index).at(group);
}

int Regex::GetGroupsCount()
{
	return groupCounts;
}
